import { Component, ViewChild, Injector, Output, 
    EventEmitter, ElementRef , Input , OnInit } from '@angular/core';

import { WaterQualityColorServiceProxy, WaterQualityColorDto } from '@shared/service-proxies/service-proxies';
import {AppComponentBase} from '@shared/app-component-base';

import { NzModalSubject, NzModalService, NzMessageService } from 'ng-zorro-antd';

import * as _ from 'lodash';

@Component({
    selector: 'createColorModalComponent',
    templateUrl: './create-color-modal.component.html'
})
export class CreateColorModalComponent extends AppComponentBase
implements OnInit {

    @Input() id: string;

    active = false;
    saving = false;

    color: WaterQualityColorDto = new WaterQualityColorDto();

    constructor(
        injector: Injector,
        private _service: WaterQualityColorServiceProxy,
        private subject: NzModalSubject
    ) {
        super(injector);
    }

    marks: any = {
        1  : 'I',
        2 : 'II',
        3 : 'III',
        4: 'IV',
        5: 'V',
        6: '劣V'
      };

    selectValues=[1,1];

    show(id: string): void {
        const self = this;
        self.active = true;

        self._service.getWaterQualityColorForEdit(id)
            .subscribe((result)=>{
                console.log(result);
                this.color = result;
                this.selectValues=[this.color.minValue,this.color.maxValue];
            });
    }

    onShown(): void {
        
    }

    save(): void {
        const self = this;

        this.saving = true;

        this.color.maxValue=_.max(this.selectValues);
        this.color.minValue=_.min(this.selectValues);

        this._service.saveWaterQualityColor(this.color)
            .finally(() => this.saving = false)
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.subject.destroy('onOk');
            });
    }

    close(): void {
        this.subject.destroy();
    }

    ngOnInit() {
        this.show(this.id);
    }
}
